var User = require('../model/user')
var Token = require('../model/token')
var crypto = require('crypto')
var ErrorCode = require('./errorCode')
var userApi = {
        login:function(req,res){
            var phone = req.body.phone
            var password = req.body.password
            User.findOne({phone:phone,password:password},function(err,user){
                if (!user){
                    res.json(400,ErrorCode.UserNotExist)
                    return
                }else{
                    var sha1 = crypto.createHash('sha1')
                    sha1.update(phone+':'+password+':'+Date.now())
                    var tokenStr = sha1.digest('hex')
                    Token.findOne({userid:user._id},function(err,token){
                        if (token){
                            token.token = tokenStr
                            token.save(function(){
                                res.json(token)
                                return
                            })
                        }else{
                            Token.create({userid:user._id,token:tokenStr},function(err,token){
                                    res.json(token)
                                    return
                            })
                        }

                    })
                }

            })
        }
        ,
        register:function(req,res){
            var phone = req.body.phone
            var password = req.body.password
            var type = req.body.type
            var info = req.body.info
            var name = req.body.name
            var iconP = req.files.icon
            if(!info){
                info = "没有简介"
            }
            if(!name){
                name = 'user'+phone
            }

            var icon
            if(iconP){
                icon = iconP.path
            }else{
                icon = "/"
            }
            User.findOne({phone:phone},function(err,user){
                if(user){
                    res.json(400,ErrorCode.PhoneExist)
                    return
                }
                if(phone.length != 11){
                    res.json(400,ErrorCode.PhoneError)
                    return
                }
                if(password.length < 6){
                    res.json(400,ErrorCode.PassWordLess)
                    return
                }
                User.create({name:name,phone:phone,password:password,icon:icon,type:type,info:info},function(err,user){
                    if(err){
                        res.json(400,ErrorCode.CreateUserError)
                        return
                    }
                    res.json(user)
                    return
                })
                
            })
        },
        updateUser:function(req,res){
            
            var userid = req.userid
            var desc = req.body.info
            var name = req.body.name
            var iconP = req.files.icon
            var icon
            if(iconP){
                icon = iconP.path
            }
            User.findOne({_id:userid},function(err,user){
                if(user){
                    user.info = desc
                    user.name = name
                    if(icon){
                        user.icon = icon
                    }
                    user.save(function(err){
                        res.json(user)
                    })
                }else{
                    res.json(ErrorCode.UserNotExist)
                    return
                }
            })
        }
        ,
        listUser:function(req,res){
            User.find(function(err,users){
                res.json(users)
                return
            })
        },
        me:function(req,res){
            User.findOne({_id:req.userid},function(err,user){
                if(user){
                    res.json(user)
                }else{
                    res.json(400,ErrorCode.CommonError)
                }
            })
        }

}
module.exports = userApi